package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsTourismProjectActivitiesDetail;
import com.macro.mall.dto.BmsTourismProjectActivitiesParam;
import com.macro.mall.mapper.BmsTourismProjectActivitiesMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.BmsTourismProjectActivitiesPicService;
import com.macro.mall.service.BmsTourismProjectActivitiesService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class BmsTourismProjectActivitiesServiceImpl implements BmsTourismProjectActivitiesService {

    @Autowired
    private BmsTourismProjectActivitiesMapper bmsTourismProjectActivitiesMapper;

    @Autowired
    private BmsTourismProjectActivitiesPicService bmsTourismProjectActivitiesPicService;

    @Autowired
    private UserUtils userUtils;

    @Override
    public List<BmsTourismProjectActivities> getList(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        if (!bmsTourismProjectActivitiesParam.isShowAll() && Objects.isNull(bmsTourismProjectActivitiesParam.getTourismProjectId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsTourismProjectActivitiesParam.getPageNum();
        Integer pageSize = bmsTourismProjectActivitiesParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<BmsTourismProjectActivities> list = bmsTourismProjectActivitiesMapper.selectListByQuery(bmsTourismProjectActivitiesParam);
        return list;
    }

    /**
     * 根据项目id获取文旅项目-活动
     */
    @Override
    public List<BmsTourismProjectActivities> getListByTourismProjectId(Long id){
        BmsTourismProjectActivitiesExample example = new BmsTourismProjectActivitiesExample();
        BmsTourismProjectActivitiesExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        if (Objects.nonNull(id)) {
            criteria.andTourismProjectIdEqualTo(id);
        }
        List<BmsTourismProjectActivities> list = bmsTourismProjectActivitiesMapper.selectByExample(example);
        return list;
    }

    @Override
    public int create(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        BmsTourismProjectActivities bmsTourismProjectActivities = new BmsTourismProjectActivities();
        BeanUtils.copyProperties(bmsTourismProjectActivitiesParam, bmsTourismProjectActivities);
        Long userId = userUtils.getUserId();
        bmsTourismProjectActivities.setCreateUser(userId);
        bmsTourismProjectActivities.setUpdateUser(userId);
        bmsTourismProjectActivities.setCreateTime(new DateTime());
        bmsTourismProjectActivities.setUpdateTime(new DateTime());
        int count = bmsTourismProjectActivitiesMapper.insertSelective(bmsTourismProjectActivities);

        String coverPic = bmsTourismProjectActivitiesParam.getCoverPic();
        List<String> picList = bmsTourismProjectActivitiesParam.getPicList();
        bmsTourismProjectActivitiesPicService.create(bmsTourismProjectActivities.getId(), coverPic, picList);

        return count;
    }

    @Override
    public int update(BmsTourismProjectActivitiesParam bmsTourismProjectActivitiesParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsTourismProjectActivities bmsTourismProjectActivities = new BmsTourismProjectActivities();
        bmsTourismProjectActivities.setId(bmsTourismProjectActivitiesParam.getId());
        BeanUtils.copyProperties(bmsTourismProjectActivitiesParam, bmsTourismProjectActivities);
        bmsTourismProjectActivities.setUpdateTime(new DateTime());
        bmsTourismProjectActivities.setUpdateUser(userId);
        int conut = bmsTourismProjectActivitiesMapper.updateByPrimaryKeySelective(bmsTourismProjectActivities);

        bmsTourismProjectActivitiesPicService.deleteByActivitiesId(bmsTourismProjectActivitiesParam.getId());

        String coverPic = bmsTourismProjectActivitiesParam.getCoverPic();
        List<String> picList = bmsTourismProjectActivitiesParam.getPicList();
        bmsTourismProjectActivitiesPicService.create(bmsTourismProjectActivities.getId(), coverPic, picList);
        return conut;
    }

    @Override
    public BmsTourismProjectActivitiesDetail getItem(Long id) {

        BmsTourismProjectActivities bmsTourismProjectActivities = bmsTourismProjectActivitiesMapper.selectByPrimaryKey(id);
        BmsTourismProjectActivitiesDetail detail = new BmsTourismProjectActivitiesDetail();
        BeanUtils.copyProperties(bmsTourismProjectActivities, detail);
        // 获取封面图
        String coverPic = bmsTourismProjectActivitiesPicService.findCoverPicByActivitiesId(bmsTourismProjectActivities.getId());
        // 获取活动图片
        List<String> picList = bmsTourismProjectActivitiesPicService.findPicListByActivitiesId(bmsTourismProjectActivities.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Override
    public int delete(List<Long> ids) {
        BmsTourismProjectActivitiesExample example = new BmsTourismProjectActivitiesExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        BmsTourismProjectActivities updateDetail = new BmsTourismProjectActivities();
        updateDetail.setUpdateUser(userId);
        updateDetail.setUpdateTime(new Date());
        updateDetail.setIsDelete((byte) 1);
        return bmsTourismProjectActivitiesMapper.updateByExampleSelective(updateDetail,example);
    }
}


